package com.example.myapplication.customizeView.keypad;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.customizeView.utils.SizeUtils;

public class KeypadView extends ViewGroup {

    public final static int DEFAULT_ROW =4;
    public final static int DEFAULT_COLUMN =3;
    public final  int DEFAULT_MARGIN =SizeUtils.dip2px(getContext(),2);

    private int textColor;
    private float textSize;
    private int itemNormalBg;
    private int itemPressBg;
    private int row = DEFAULT_ROW;
    private int column = DEFAULT_COLUMN;
    private int mItemMargin;
    private OnKeyItemClickListener mOnKeyItemClick = null;

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public int getItemNormalBg() {
        return itemNormalBg;
    }

    public void setItemNormalBg(int itemNormalBg) {
        this.itemNormalBg = itemNormalBg;
    }

    public int getItemPressBg() {
        return itemPressBg;
    }

    public void setItemPressBg(int itemPressBg) {
        this.itemPressBg = itemPressBg;
    }

    public int getItemMargin() {
        return mItemMargin;
    }

    public void setItemMargin(int mItemMargin) {
        this.mItemMargin = mItemMargin;
    }

    public KeypadView(Context context) {
        this(context,null);
    }

    public KeypadView(Context context, AttributeSet attrs) {
       this(context, attrs,0);
    }

    public KeypadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        setupItem();

    }

    private void setupItem() {
        removeAllViews();
        for (int i = 0; i < 11; i++) {
            TextView item = new TextView(getContext());
            //内容
            if (i==10){
                item.setTag(true);
                item.setText("删除");
            }else{
                item.setTag(false);
                item.setText(String.valueOf(i));
            }
            //居中
            item.setGravity(Gravity.CENTER);
            //大小
            if (textSize!=-1) {
                item.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
            }
            //字体颜色
            item.setTextColor(textColor);
            //背景
            item.setBackground(providerItemBg());
            item.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnKeyItemClick != null) {
                        Boolean isDelete = (Boolean) v.getTag();
                        if (isDelete){

                            mOnKeyItemClick.OnDeleteClick();
                        }else{
                            String valueText = ((TextView) v).getText().toString();
                            mOnKeyItemClick.OnNumberClick(Integer.parseInt(valueText));
                        }

                    }
                }
            });
            addView(item);
        }
    }

    private Drawable providerItemBg() {
        StateListDrawable bg = new StateListDrawable();
        //按下去的背景
        GradientDrawable pressDrawable = new GradientDrawable();
        pressDrawable.setCornerRadius(SizeUtils.dip2px(getContext(),5));
        pressDrawable.setColor(itemPressBg);
        bg.addState(new int[]{android.R.attr.state_pressed},pressDrawable);
        //常规状态下的背景
        GradientDrawable normalDrawable = new GradientDrawable();
        normalDrawable.setColor(itemNormalBg);
        normalDrawable.setCornerRadius(SizeUtils.dip2px(getContext(),5));
        bg.addState(new int[]{},normalDrawable);

        return bg;

    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeypadView);
        //获取属性
        //NumberColor
        textColor = a.getColor(R.styleable.KeypadView_NumberColor, context.getResources().getColor(R.color.white));
        //NumberSize
        textSize = a.getDimensionPixelSize(R.styleable.KeypadView_NumberSize, 16);
        //NumberBg
        itemNormalBg = a.getColor(R.styleable.KeypadView_ItemNormalBg, context.getResources().getColor(R.color.key_item_color));
        itemPressBg = a.getColor(R.styleable.KeypadView_ItemPressBg, context.getResources().getColor(R.color.key_item_press_color));
        //itemMargin
        mItemMargin = a.getDimensionPixelSize(R.styleable.KeypadView_itemMargin, DEFAULT_MARGIN);
        a.recycle();
    }

    /**
     * 测量
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int verticalPadding = getPaddingTop()+getPaddingBottom();
        int horizontalPadding = getPaddingLeft()+getPaddingRight();
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        MeasureSpec.getMode(widthMeasureSpec);
        MeasureSpec.getMode(heightMeasureSpec);

        int preItemWidth = (widthSize-(column+1)*mItemMargin-horizontalPadding) / column;
        int preItemHeight = (heightSize-(row+1)*mItemMargin-verticalPadding)/row;
        int normalWidthSpec = MeasureSpec.makeMeasureSpec(preItemWidth, MeasureSpec.EXACTLY);
        int deleteWidthSpec = MeasureSpec.makeMeasureSpec(preItemWidth*2+mItemMargin, MeasureSpec.EXACTLY);
        int heightSpec = MeasureSpec.makeMeasureSpec(preItemHeight, MeasureSpec.EXACTLY);

        //测量子View
        for (int i = 0; i < getChildCount(); i++) {
            View item = getChildAt(i);
            Boolean isDelete = (Boolean) item.getTag();
            item.measure(isDelete?deleteWidthSpec:normalWidthSpec,heightSpec);
        }

        //测量自己
        setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int childCount = getChildCount();
        int left = mItemMargin+paddingLeft,right,top,bottom;
        for (int i = 0; i < childCount; i++) {
            //求出当前元素在第几列第几行
            int rowIndex = i/column;
            int columnIndex = i % column;
            if (columnIndex==0){
                left = mItemMargin+paddingLeft;
            }
            View item = getChildAt(i);
            top = rowIndex*item.getMeasuredHeight()+mItemMargin*(rowIndex+1)+paddingTop;
            right= left+item.getMeasuredWidth();
            bottom = top+item.getMeasuredHeight();
            item.layout(left,top,right,bottom);
            left += item.getMeasuredWidth()+mItemMargin;
        }


    }

    public void setOnKeyItemClickListener(OnKeyItemClickListener listener){
        this.mOnKeyItemClick = listener;

    }
    public interface OnKeyItemClickListener{
        void OnNumberClick(int value);
        void OnDeleteClick();
    }
}
